package com.foreveross.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.SYWebService.SYHttpclientCall;
import com.foreveross.SYWebService.vo.ConditionVo;
import com.foreveross.SYWebService.vo.DataVo;
import com.foreveross.SYWebService.vo.SYResult;
import com.foreveross.SYWebService.vo.SYSWin;
import com.foreveross.common.Constants;
import com.foreveross.dao.ExpressDAO;
import com.foreveross.dao.HouseUserDAO;
import com.foreveross.util.ObjectToMap;
import com.foreveross.vo.Express;
import com.foreveross.vo.HouseAddress;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.HrCode;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.Tenement;
import com.foreveross.vo.TenementDepartment;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Service("houseUserService")
@Transactional
public class HouseUserService extends BaseService {

	@Resource
	public HouseUserDAO houseUserDAO;

	public List<HouseUser> getMyHouseUserList(String weixinId,
			String authenticationStatus, int pageIndex, int pageSize) throws Exception {
		return houseUserDAO.getMyHouseUserList(weixinId, authenticationStatus, pageIndex, pageSize);
	}
	
	public long getMyHouseUserListCount(String weixinId,
			String authenticationStatus) throws Exception {
		return houseUserDAO.getMyHouseUserListCount(weixinId, authenticationStatus);
	}

	/**
	 * 获取用户手机号码列表
	 * 
	 * @param conditionVo
	 */
	public Map<String, Object> userOwnerVerify(ConditionVo conditionVo) throws Exception {
//		DataVo datavo = null;
		Map<String, String> params = new HashMap<String, String>();
		params = ObjectToMap.objectToMapViaFields(conditionVo, true);
		SYResult sysResult = SYHttpclientCall.getSYData(params);
		List<SYSWin> list = sysResult.getUserRev_GetOwnerInfor();
		Map<String, Object> map = new HashMap<String, Object>();
		if (list != null && !list.isEmpty()) {
			SYSWin sYSWin = list.get(0);
			List<DataVo> listv = sYSWin.getSyswin();
			if (listv != null && !listv.isEmpty()) {
				map.put("data", listv);
				map.put("total", listv.size());
			}
		}
		return map;
	}

	public List<HouseAddress> ArrayToObject(List list) {
		List<HouseAddress> listv = new ArrayList<HouseAddress>();
		for (HouseAddress houseAddress : listv) {
			HouseAddress h = new HouseAddress();
			h.setAddress(houseAddress.getAddress());
			h.setHome(houseAddress.getHome());
			h.setHouseId(houseAddress.getHouseId());
			h.setHouseUnit(houseAddress.getHouseUnit());
			h.setName(houseAddress.getName());
			h.setId(houseAddress.getId());
			listv.add(h);
		}
		return listv;
	}

	public List<HouseAddress> getHouseWYAddressList(String weixinId,
			String pageIndex, String pageSize) throws Exception {
		List list = houseUserDAO.getHouseWYAddressList(weixinId, pageIndex,
				pageSize);
		return ArrayToObject(list);
	}

	public HouseUser getHouseUserByweixinId(String weixinId) throws Exception {
		List list = houseUserDAO.getHouseUserByweixinId(weixinId);
		if (list != null && !list.isEmpty())
			return (HouseUser) list.get(0);
		return null;
	}

	public boolean isAuthentication(String weixinId) throws Exception {
		List list = houseUserDAO.getHouseUserByweixinId(weixinId);
		if (list != null && !list.isEmpty())
			return true;
		return false;
	}
	//TODO 物业电话处理
	public Map<String,Object> getTenementPhone(String weixinId)
			throws Exception {
		List<Tenement> list = houseUserDAO.getUserTenement(weixinId);
		List<Map<String,Object>> listv = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = new HashMap<String,Object>();
		for (Tenement tenement : list) {
			for(TenementDepartment tenementDepartment :tenement.getTenementDepartmentList()){
				Map<String,Object> mapO = new HashMap<String,Object>();
				mapO.put("name", tenementDepartment.getName());
				mapO.put("phone", tenementDepartment.getPhone());
				mapO.put("address", tenementDepartment.getAddress());
				mapO.put("telephone", tenementDepartment.getTelephone());
				listv.add(mapO);
			}
		}
		map.put("data", listv);
		map.put("total", listv.size());
		return map;
	}

	public List<HouseUser> findHouseUserList(String name, String house_id,
			String telepnoe, String authentication_status, String type,
			int pageIndex, int pageSize, String userId) throws Exception {
		return houseUserDAO.findHouseUserList(name, house_id, telepnoe,
				authentication_status, type, pageIndex, pageSize, userId);
	}

	public long getHouseUserListCount(String name, String house_id,
			String telepnoe, String authentication_status, String type,
			String userId) throws Exception {
		return houseUserDAO.getHouseUserListCount(name, house_id, telepnoe,
				authentication_status, type, userId);
	}

	public void deleteHouseUserList(String houseUserids) throws Exception {
		for (String houseUserid : houseUserids.split(",")) {
			houseUserDAO.deleteHouseUser(houseUserid);
		}
	}

	public void authentication(HouseUser houseUser) throws Exception {
		houseUserDAO.authentication(houseUser);
	}

	public String getWexinUserName(String weixinid) throws Exception {
		return houseUserDAO.getWexinUserName(weixinid);
	}

	public HouseUser getHouseUser(String userId) throws Exception {
		HouseUser user = (HouseUser) houseUserDAO.findById(HouseUser.class,
				userId);
		if (user != null && user.getHrHouses().getProvince() != null) {
			HrCode c = (HrCode) houseUserDAO.findById(HrCode.class, user
					.getHrHouses().getProvince().getCodeId());
			if (c != null)
				user.setCodeDesc(c.getCodeDesc());
		}
		return user;
	}

	public String getTenementId(String userId) throws Exception {
		return houseUserDAO.getTenement(userId);
	}
	public long getHouseUserListCount(String weixinId,String codeId) throws Exception {
		return houseUserDAO.getHouseUserListCount(weixinId, codeId);
	}
	
	public List<Map<String, String>> getHouseUserList(String weixinId,String codeId,
			int pageIndex, int pageSize) throws Exception {
		List<HouseUser> houseUserList = houseUserDAO.getHouseUserList(weixinId, codeId, pageIndex, pageSize);
		
		List<Map<String, String>> listv = new ArrayList<Map<String, String>>();
		for (HouseUser hc : houseUserList) {
			Map<String, String> map = new HashMap<String, String>();
			String str = hc.getHouseId() + "=" + hc.getName() + "="
					+ hc.getAddress() + "=" + hc.getId() + "="
					+ hc.getTelephone();
			map.put("chooseItem", hc.getAddress());
			map.put("chooseValue", str);
			listv.add(map);
		}

		return listv;
	}

	public List<Map<String, String>> getMyTenementList(String weixinId, String authenticationStatus,
			int pageIndex,int pageSize)
			throws Exception {
		List<HouseUser> houseUserList = houseUserDAO.getMyHouseUserList(weixinId, authenticationStatus, pageIndex, pageSize);
		
		List<Map<String, String>> listv = new ArrayList<Map<String, String>>();
		for (HouseUser hc : houseUserList) {
			Map<String, String> map = new HashMap<String, String>();
			String str = hc.getHouseId() + "=" + hc.getName() + "="
					+ hc.getAddress() + "=" + hc.getId() + "="
					+ hc.getTelephone();
			map.put("chooseItem", hc.getAddress());
			map.put("chooseValue", str);
			listv.add(map);
		}

		return listv;
	}

}
